package cn.yunzhisheng.vui.assistant.tv;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class VuiVoiceHandleActivity extends FragmentActivity implements VuiVoiceHandleCallback {

    protected VuiVoiceHandleImpl mVuiVoiceHandle;

    public boolean go2VodType(String type) {
        try {
            Intent intent = new Intent("myvst.intent.action.VodTypeActivity");
            intent.putExtra("vodtype", type).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        } catch (Exception e) {

        }
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mVuiVoiceHandle = new VuiVoiceHandleImpl(this);
        mVuiVoiceHandle.setVuiVoiceHandleCallbackk(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onStart() {
        if (mVuiVoiceHandle != null) {
            mVuiVoiceHandle.connectService();
        }
        super.onStart();
    }

    @Override
    protected void onStop() {
        if (mVuiVoiceHandle != null) {
            mVuiVoiceHandle.disconnectService();
        }
        super.onStop();
    }

    public boolean vui_voiceMessage(String msg) {
        android.util.Log.i("jesus", "onVoiceMessage()" + msg);
        if ("上一个频道".equals(msg) || "上一个台".equals(msg)) {
            return vui_doPreChannel();
        } else if ("下一个频道".equals(msg) || "下一个台".equals(msg) || "换台".equals(msg)) {
            return vui_doNextChannel();
        } else if ("电视列表".equals(msg) || "节目列表".equals(msg)) {
            return vui_doShowEpg();
        } else if ("返回".equals(msg)) {
            return vui_doBack();
        } else if ("上一页".equals(msg)) {
            return vui_doPageUp();
        } else if ("下一页".equals(msg)) {
            return vui_doPageDown();
        } else if ("播放".equals(msg)) {
            return vui_doPlay();
        } else if ("暂停".equals(msg)) {
            return vui_doPause();
        } else if ("快进".equals(msg)) {
            return vui_doFastForward();
        } else if ("快退".equals(msg)) {
            return vui_doBackForward();
        } else if ("退出".equals(msg)) {
            return vui_exit();
        } else if ("电影".equals(msg) || msg.contains("看电影")) {
            return go2VodType("1");
        } else if ("电视剧".equals(msg) || msg.contains("看电视剧")) {
            return go2VodType("2");
        } else if ("综艺".equals(msg) || msg.contains("看综艺")) {
            return go2VodType("4");
        } else if ("动漫".equals(msg) || msg.contains("看动漫")) {
            return go2VodType("3");
        }

        if (msg.contains("导演的")) {
            go2Search(msg, "导演的", "dir");
            return true;
        } else if (msg.contains("主演的")) {
            go2Search(msg, "主演的", "act");
            return true;
        }
        return false;
    }

    private void go2Search(String msg, String prefix, String type) {
        try {
            String word = msg.substring(0, msg.indexOf(prefix));
            startActivity(new Intent("myvst.intent.action.SearchActivity").putExtra("search_word", word)
                    .putExtra("type", type).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
        } catch (Exception e) {

        }
    }

    protected boolean vui_exit() {
        finish();
        return true;
    }

    protected boolean vui_doPlay() {
        return true;
    }

    protected boolean vui_doPause() {
        return true;
    }

    protected boolean vui_doPageUp() {
        return true;
    }

    protected boolean vui_doPageDown() {
        return true;
    }

    protected boolean vui_doFastForward() {
        return true;
    }

    protected boolean vui_doBackForward() {
        return true;
    }

    protected boolean vui_doPreChannel() {
        return true;
    }

    protected boolean vui_doNextChannel() {
        return true;
    }

    public boolean vui_doShowEpg() {
        return true;
    }

    protected boolean vui_doBack() {
        return true;
    }

    protected boolean vui_doChangeSource() {
        return true;
    }

    public boolean isConnectedClient() {
        return mVuiVoiceHandle != null && mVuiVoiceHandle.isConnectedClient();
    }
}
